package com.hdoit.ares.logical.personnel.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.hdoit.ares.logical.personnel.dao.IPersonnelDao;
import com.hdoit.ares.logical.personnel.model.Personnel;
import com.hdoit.ares.logical.personnel.service.IPersonnelService;
import com.hdoit.ares.logical.viewmodel.personnel.PersonnelSearch;
import com.tennetcn.common.dao.base.impl.SuperService;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.enums.pub.YesOrNoInteger;
import com.tennetcn.common.logical.superbase.model.RoleEmployee;
import com.tennetcn.common.logical.superbase.service.IEmployeeService;
import com.tennetcn.common.logical.superbase.service.IRoleEmployeeService;
import com.tennetcn.common.logical.superbase.service.IRoleService;
import com.tennetcn.common.message.DaoBaseException;
import com.tennetcn.common.message.DaoBaseRuntimeException;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.CommonUtils;

/**
 * @author      caochengmeng
 * @email       
 * @createtime  2017年11月15日 10:09:24
 * @comment
 */
@Component
public class PersonnelServiceImpl extends SuperService<Personnel> implements IPersonnelService{
	
	private IPersonnelDao personnelDao;
	
	public IPersonnelDao getPersonnelDao() {
		return personnelDao;
	}

	@Resource
	public void setPersonnelDao(IPersonnelDao personnelDao) {
		this.personnelDao = personnelDao;
	}

	private IEmployeeService employeeService;

	public IEmployeeService getEmployeeService() {
		return employeeService;
	}

	@Resource
	public void setEmployeeService(IEmployeeService employeeService) {
		this.employeeService = employeeService;
	}

	private IRoleService roleService;

	public IRoleService getRoleService() {
		return roleService;
	}

	@Resource
	public void setRoleService(IRoleService roleService) {
		this.roleService = roleService;
	}
	
	private IRoleEmployeeService roleEmployeeService;

	public IRoleEmployeeService getRoleEmployeeService() {
		return roleEmployeeService;
	}

	@Resource
	public void setRoleEmployeeService(IRoleEmployeeService roleEmployeeService) {
		this.roleEmployeeService = roleEmployeeService;
	}

	@Override
	public boolean applyChange(Personnel personnel) {
/*		List<Role> roles=roleService.queryListByMark(RoleMapper.personnel);
		List<String> roleIdList=Enumerable.init(roles).map(role->role.getRoleId()).toList();
		personnel.setRoleIds(StringHelper.join(roleIdList));*/
		if(employeeService.applyEmployeeLogin(personnel)){
			if(!super.applyChange(personnel)){
				throw new DaoBaseRuntimeException("employeeUser applyChange roll back");
			}
			return saveRolePersonnel(personnel);
		}
		return false;
	}

	private boolean saveRolePersonnel(Personnel personnel) {
		if (personnel.getRoleIds() == null) {
			return true;
		}
		roleEmployeeService.deleteByEmployeeId(personnel.getEmployeeId());
		return saveRolePersonnel(personnel.getRoleIds(), personnel);
	}

	private boolean saveRolePersonnel(String roleIds, Personnel personnel) {
		if (StringUtils.isEmpty(roleIds)) {
			return true;
		}
		String[] roleIdStrs = roleIds.split(",");
		List<RoleEmployee> roleEmployees = new ArrayList<RoleEmployee>();
		RoleEmployee roleEmployee = null;
		for (String roleId : roleIdStrs) {
			roleEmployee = new RoleEmployee();
			roleEmployee.setId(CommonUtils.getKeyId());
			roleEmployee.setRoleId(roleId);
			roleEmployee.setEmployeeId(personnel.getEmployeeId());
			roleEmployee.setModelStatus(ModelStatus.add);
			roleEmployee.setDeleteMark(YesOrNoInteger.no);
			if (ModelStatus.add.equals(personnel.getModelStatus())) {
				roleEmployee.setCreateDate(personnel.getCreateDate());
				roleEmployee.setCreateUserId(personnel.getCreateUserId());
				roleEmployee.setCreateUserName(personnel.getCreateUserName());
			} else if (ModelStatus.update.equals(personnel.getModelStatus())) {
				roleEmployee.setCreateDate(personnel.getModifyDate());
				roleEmployee.setCreateUserId(personnel.getModifyUserId());
				roleEmployee.setCreateUserName(personnel.getModifyUserName());
			}
			roleEmployees.add(roleEmployee);
		}
		return roleEmployeeService.insertListEx(roleEmployees) == roleEmployees.size();
	}

	@Override
	public List<Personnel> queryList(PersonnelSearch personnelSearch, PagerModel pagerModel,String employeeAccount) {
		return personnelDao.queryList(personnelSearch, pagerModel, employeeAccount);
	}

	@Override
	public Personnel checkCityMan(String key) {
		Personnel personnel = null;
		try {
			personnel = personnelDao.queryModel(key);
			
		} catch (DaoBaseException e) {
			e.printStackTrace();
		}
		return personnel;
	}
}
